package com.study.talk.common.util;


import com.study.talk.common.exception.BadRequestException;
import com.study.talk.common.security.MyUserDetails;
import com.study.talk.module.entity.User;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {

    public static User getUser(){
        User user;
        try {
            Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            user = ((MyUserDetails)principal).getUser();
        }catch (Exception e){
            throw new BadRequestException(401,"无操作权限，请先登录");
        }
        if (null == user)
            throw new BadRequestException(401,"无操作权限，请先登录");
        return user;
    }
}
